package com.tianhui.consignortwofei.mvp.ui.activity;

import android.text.TextUtils;

import androidx.recyclerview.widget.RecyclerView;

import com.fgs.common.CommonActivity;
import com.fgs.common.CommonPresenter;
import com.fgs.common.CommonView;
import com.fgs.common.entity.PictureInfo;
import com.fgs.common.entity.UploadPictureInfo;
import com.fgs.common.pictureSelector.SelectPicture;
import com.fgs.common.uploadPicture.IUploadPicture;
import com.fgs.common.uploadPicture.UploadPicture;
import com.fgs.common.utils.CommonUtil;
import com.fgs.common.utils.LogUtil;
import com.luck.picture.lib.entity.LocalMedia;
import com.tianhui.consignortwofei.other.PictureType;

import java.util.ArrayList;
import java.util.List;

/**
 * @author fgs
 * @date 2020/4/2
 * @description
 **/
public abstract class DriverDefaultUpPictureActivity<V extends CommonView, P extends CommonPresenter<V>> extends CommonActivity<V, P> {


    private SelectPicture mSelectPicture;
    //单据照片
    private List<String> mSelectPictureList = new ArrayList<>();


    @Override
    public void init() {

        mSelectPicture = new SelectPicture();
        mSelectPicture.setMaxSelectNum(3);

        mSelectPicture.selectMultiPicture(this, new SelectPicture.OnSelectResultCallback() {
            @Override
            public void selectResultCallback(List<LocalMedia> selectList) {

                if (null != selectList && selectList.size() > 0) {

                    mSelectPictureList.clear();
                    int size = selectList.size();
                    String path = "";
                    for (int i = 0; i < size; i++) {
                        path = selectList.get(i).getCompressPath();
                        requestPictureUpload(PictureType.TYPE_INVOICES_IMAGE, path, i);
                    }
                }
            }

            @Override
            public void selectResultPath(String path) {

            }

            @Override
            public void deletePictureCallback(List<LocalMedia> selectList, int position) {
                int size = mSelectPictureList.size();
                LogUtil.print("position--" + position + "\n" + "size--" + size);
                if (position < size) {
                    mSelectPictureList.remove(position);
                }

            }
        }, getPictureRecyclerView());


//        Bundle bundle = getIntent().getExtras();
//        if (null != bundle && !bundle.isEmpty()) {
//            mOrderInfo = bundle.getParcelable(KeyValue.KEY_ORDER_INFO);
//            if (null != mOrderInfo) {
//                mOrderId = mOrderInfo.dispatchno;
//            }
//        }

    }


    /**
     * 单据照片
     *
     * @return
     */
    public abstract RecyclerView getPictureRecyclerView();

    /**
     * 单据照片module
     *
     * @return
     */
    public abstract String getPictureModule();

    public String getSelectPictureId() {

        return CommonUtil.getSelectPictureId(mSelectPictureList);
    }


    private void requestPictureUpload(int type, String path, int position) {
        if (TextUtils.isEmpty(path)) return;
//        String module = getPictureModule();
//        if (TextUtils.isEmpty(module)) {
//            module = "orderEvaluate";
//        }

        UploadPictureInfo uploadPictureInfo = new UploadPictureInfo();
        uploadPictureInfo.picturePath = path;
//        uploadPictureInfo.pictureModule = module;
        uploadPictureInfo.pictureModule = "dispatchSource";
        new UploadPicture(  new IUploadPicture.UploadPictureCallback() {
            @Override
            public void onSuccess(UploadPictureInfo uploadPictureInfo, PictureInfo pictureInfo) {
                processPictureUploadResult(pictureInfo, type);
            }

            @Override
            public void onFailed(UploadPictureInfo uploadPictureInfo) {
                removePicture(type, position);
            }
        }).uploadPicture(this, uploadPictureInfo);

    }

    private void removePicture(int type, int position) {
        if (type == PictureType.TYPE_INVOICES_IMAGE) {
            mSelectPicture.removePicture(position);
        }
    }

    private void processPictureUploadResult(PictureInfo pictureInfo, int type) {
        switch (type) {
            case PictureType.TYPE_INVOICES_IMAGE:
                mSelectPictureList.add(pictureInfo.id);
                break;
        }
        LogUtil.print("pictureID---" + pictureInfo.id);
    }


}
